from typing import Optional

from model.uml_package import UmlPackage
from model.uml_profile import UmlProfile
from model.uml_class import UmlClass
from model.uml_enumeration import UmlEnumeration


class UmlModel:
    def __init__(self) -> None:
        # 所有的包
        self.packages: list[UmlPackage] = []
        self.packageDict: dict[str, UmlPackage] = {}

        # 所有的profile
        self.profiles: list[UmlProfile] = []
        self.profileDict: dict[str, UmlProfile] = {}

        # 当前包
        self.package: Optional[UmlPackage] = None

        # 当前profile
        self.profile: Optional[UmlProfile] = None

        # 所有类
        self.classes: dict[str, UmlClass] = {}

        # 所有枚举
        self.enums: dict[str, UmlEnumeration] = {}

    def to_json_all(self) -> dict[str, object]:
        json: dict = {}

        packages = []
        for package in self.packages:
            packages.append(package.to_json())

        profiles = []
        for profile in self.profiles:
            profiles.append(profile.to_json())

        json["package"] = {"packages": packages, "profiles": profiles}
        return json

    def to_json(self, key: str = "") -> dict[str, object]:
        json: dict = {}

        if key == "":
            if self.package is not None:
                json["package"] = self.package.to_json()

            if self.profile is not None:
                json["profile"] = self.profile.to_json()
            return json

        package = self.packageDict.get(key)
        if package is not None:
            json["package"] = package.to_json()

        profile = self.profileDict.get(key)
        if profile is not None:
            json["profile"] = profile.to_json()

        return json
